<?php
/**
* Initialize pretty formatted response array.
* @access public
*/
function init_response()
{
    $ci = &get_instance();
    $ci->response_array = array();
    $ci->response_array['status'] = 'failed';
    $ci->response_array['code'] = '400';
}

/**
 * Retrieve search term from GET values.
 * @access public
 * @return int Returns the search term submitted, otherwise NULL.
 */
//function retrieve_get_search_term()
//{
//    $ci = &get_instance();
//    $params = $ci->uri->uri_to_assoc();
//    
//    if(isset($params['search_term']) && ! empty($params['search_term']))
//    {
//        return $params['search_term'];
//    } else
//    {
//        format_response('failed', 401, 'Invalid parameter.', array('Search term is required.'));
//    }
//}

/**
 * Retrieve search term from POST values.
 * @access public
 * @return int Returns the id submitted, otherwise NULL.
 */
//function retrieve_post_search_term()
//{
//    $ci = &get_instance();
//    $ci->load->library('form_validation');
//    $ci->form_validation->set_error_delimiters('', '');
//    
//    $ci->form_validation->set_rules('search_term', 'search_term', 'trim|required|xss_clean');
//    if ($ci->form_validation->run() == TRUE)
//    {
//        return set_value('search_term');
//    } else
//    {
//        format_response('failed', 401, 'Invalid parameter.', array('Search term is required.'));
//    }
//}

/**
 * Retrieve id from GET values.
 * @access public
 * @return int Returns the id submitted, otherwise NULL.
 */
//function retrieve_get_id()
//{
//    $ci = &get_instance();
//    $params = $ci->uri->uri_to_assoc();
//    
//    if(isset($params['id']) && ! empty($params['id']) && is_numeric($params['id']))
//    {
//        return $params['id'];
//    }
//    else
//    {
//        format_response('failed', 401, 'Invalid parameter.', array('Invalid ID.'));
//    }
//}

/**
 * Retrieve id from POST values.
 * @access public
 * @return int Returns the id submitted, otherwise NULL.
 */
//function retrieve_post_id()
//{
//    $ci = &get_instance();
//    $ci->load->library('form_validation');
//    $ci->form_validation->set_error_delimiters('', '');
//    
//    $ci->form_validation->set_rules('id', 'id', 'trim|required|numeric|xss_clean');
//    if ($ci->form_validation->run() == TRUE)
//    {
//        return set_value('id');
//    }
//    else
//    {
//        format_response('failed', 401, 'Invalid parameter.', array('Invalid ID.'));
//    }
//}

/**
 * Retrieve get values.
 * @access public
 * @return array Returns an array of get values. Use defaults if nothing is submitted.
 */
function retrieve_get_values()
{
    $ci = &get_instance();
    $params = $ci->uri->uri_to_assoc();

    $data = array();
    $data['limit'] = isset($params['limit']) ? $params['limit'] : NULL;
    $data['offset'] = isset($params['offset']) ? $params['offset'] : 0;
    $data['platform'] = isset($params['platform']) ? $params['platform'] : 'iOS';
    $data['format'] = isset($params['format']) ? $params['format'] : 'xml';
    
    $errors = array();
    // check limit if valid
    if(isset($params['limit']) && ! is_numeric($data['limit']))
        array_push($errors, 'Limit should only contain numeric values.');
    
    // check offset if valid
    if(isset($params['offset']) && ! is_numeric($data['offset']))
        array_push($errors, 'Offset should only contain numeric values.');
    
    // check if valid platform
    if(isset($params['platform']) && (strtolower($params['platform']) != 'ios' && strtolower($params['platform']) != 'android'))
        array_push($errors, 'Platform not supported.');
    
    // check if valid platform
    if(isset($params['format']) && (strtolower($params['format']) != 'xml' && strtolower($params['format']) != 'json') && strtolower($params['format']) != 'jsonp')
        array_push($errors, 'Format not supported.');

    // check if we have errors
    if(count($errors) > 0)
    {
        $ci->response_array['status'] = 'failed';
        $ci->response_array['code'] = '402';
        $ci->response_array['message'] = 'Invalid Parameter.';
        $ci->response_array['errors'] = $errors;
        $ci->response($ci->response_array, 402);
    }
    else
    {
        return $data;
    }
}

/**
 * Retrieve post values
 * @access public
 * @return array Returns an array of posted values. Use defaults if nothing is submitted.
 */
function retrieve_post_values()
{
    $data = array();
    $data['platform'] = set_value('platform') ? set_value('platform') : 'iOS';
    $data['limit'] = set_value('limit') ? set_value('limit') : NULL;
    $data['offset'] = set_value('offset') ? set_value('offset') : 0;
    return $data;
}

/**
 * Validate user post values.
 * @access public
 * @return boolean True if all fields passed the validation. Otherwise false
 */
function valid()
{
    $ci = &get_instance();
    $ci->load->library('form_validation');
    $ci->form_validation->set_error_delimiters('', '');
    
    $ci->form_validation->set_rules('platform', 'platform', 'trim||xss_clean');
    $ci->form_validation->set_rules('limit', 'limit', 'trim|numeric|xss_clean');
    $ci->form_validation->set_rules('offset', 'offset', 'trim|numeric|xss_clean');

    if ($ci->form_validation->run() == TRUE)
    {
        return true;
    }
    else
    {
        format_response('failed', 402, 'Invalid parameter.', $ci->form_validation->error_array());
    }
}

/**
 * Format response
 * @param string $status Status either success or failed
 * @param int $code Status codes
 * @param string $message Message to be shown.
 * @param array $errors An array of errors, if any.
 * @param array $items An array of items, if any.
 */
function format_response($status = 'error', $code = '400', $message = 'Request Failed', $errors = NULL, $items = NULL)
{
    $ci = &get_instance();
    
    $ci->response_array['status'] = $status;
    $ci->response_array['code'] = $code;
    $ci->response_array['message'] = $message;
    
    if($errors)
        $ci->response_array['errors'] = $errors;
    
    if($items)
        $ci->response_array['items'] = $items;
    
    $ci->response($ci->response_array, $code);
}

/**
 * Formats string for valid json_encode
 * @param string $data String to be encoded
 * @return string Returns string with valid html characters
 */
function clean($data)
{
    $encode = mb_check_encoding($data, 'UTF-8') ? $data : utf8_encode($data);	
    $decoded = html_entity_decode($encode, ENT_QUOTES, 'utf-8');
    return $decoded;
}

/* End of file param_helper.php */
/* Location: ./application/modules/backend-services/helpers/param_helper.php */